package com.bocloud.cmp.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.bocloud.cmp.dao.SnapshotDao;
import com.bocloud.cmp.entity.resource.Snapshot;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 快照DAO接口实现
 * 
 * @author dongkai
 *
 */
@Repository("snapshotDao")
public class SnapshotDaoImpl extends JdbcGenericDao<Snapshot, Long> implements SnapshotDao {

	@Override
	public List<Snapshot> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select s.*,c.name vm_name from snapshot s left join cloud_server c on s.vm_id = c.id where s.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, page, rows, params, sorter, "s");
		return this.list(Snapshot.class, sql);
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from snapshot s where s.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, params, null, "s");
		Map<String, Object> param = SQLHelper.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	@Override
	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update snapshot set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	@Override
	public List<Snapshot> listByVid(Long vendorId) throws Exception {
		String sql = "select * from snapshot where is_deleted = 0 and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(Snapshot.class, sql, params);
	}

	@Override
	public List<Snapshot> listByVMid(Long vmId) throws Exception {
		String sql = "select * from snapshot where is_deleted = 0 and vm_id = :vmId";
		Map<String, Object> params = MapTools.simpleMap("vmId", vmId);
		return this.list(Snapshot.class, sql, params);
	}

	@Override
	public Snapshot getBySnapId(String snapId) throws Exception {
		String sql = "select * from snapshot where is_deleted = 0 and snapshot_id = :snapId";
		Map<String, Object> param = MapTools.simpleMap("snapId", snapId);
		List<Snapshot> list = this.list(Snapshot.class, sql, param);
		Snapshot snap = new Snapshot();
		if (!list.isEmpty()) {
			snap = list.get(0);
		}
		return snap;
	}

	@Override
	public Snapshot query(Long id) throws Exception {
		String sql = "select * from snapshot where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Snapshot> list = this.list(Snapshot.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "update snapshot set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	@Override
	public List<Snapshot> listByVloumeId(String volumeId) throws Exception {
		String sql = "select * from snapshot where is_deleted = 0 and volume_id = :volumeId";
		Map<String, Object> params = MapTools.simpleMap("volumeId", volumeId);
		return this.list(Snapshot.class, sql, params);
	}

	@Override
	public boolean removeByVm(Long vmId, Long userId) throws Exception {
		String sql = "update snapshot set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vm_id = :vmId";
		Map<String, Object> params = MapTools.simpleMap("vmId", vmId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	@Override
	public int count(List<Param> params, String type) throws Exception {
		String sql = "select count(1) from snapshot s,cloud_vendor c where s.is_deleted = 0 and s.vendor_id = c.id";
		if (!StringUtils.isEmpty(type)) {
			sql += " and c.type = '" + type + "'";
		}
		sql = SQLHelper.buildRawSql(sql, params, null, "s");
		Map<String, Object> param = SQLHelper.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

}
